/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iagente;

import static iagente.MatrizQ.AGENTE;
import static iagente.MatrizQ.FINAL;
import static iagente.MatrizQ.NEUTRO;
import static iagente.MatrizQ.PARED;
import static iagente.MatrizQ.abajo;
import static iagente.MatrizQ.abajoder;
import static iagente.MatrizQ.abajoizq;
import static iagente.MatrizQ.arriba;
import static iagente.MatrizQ.arribader;
import static iagente.MatrizQ.arribaizq;
import static iagente.MatrizQ.derecha;
import static iagente.MatrizQ.izquierda;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


/**
 *
 * @author Note
 */
public class Interfaz extends javax.swing.JFrame implements ActionListener {

        //Arreglo de botones que se usaran como iconos
	public Dibujo[] iconos;
	
        // Instancia para el grafico de la matriz Q
	MatrizQVisual qwindow;
        
       //Mapa de caracteres que se utilizaran para graficar
        public static Mapa mapa;
        
        //se utiliza para identificar el tipo de estado
        public static int estados;
        
        //se utiliza para saber cuando se alcanza una pared o una meta
        private boolean llegada;
        private boolean peligro;
        
        //variables utilizadas para el tamaño de la ventana
        public static int x_fila;
        public static int y_colu;
        public static int tamaño;
                 
        //Atributos necesarios para la simulacion
        public static double gamma;
	public static int episodios;
	public static int estadofinal;
	public static int pozo;
        public static int excelente;
        public static int buena;
        public static int mala;
        
        public static double eptau; //variable para gamma o epsilon
        public static String cargaAleatManual; //variable para el tipo de seteo aleatorio o manual
        public static int politica; //tipo de politica de seleccion

	// Atributo necesario para animacion de la simulacion
	private Timer animationTimer;
	
	// Update board in every 1 second = 10 miliseconds.
	private final int animationDelay = 10;    

        private int animCount; // Animation count
	private boolean setreset = true; // Used for blinking...

	// Some String constants used for information box in the GUI
	private final String initStr    = "<html><br>LISTO PARA APRENDER</html>";
	private final String loadingStr = "<html>PLEASE WAIT<br>Aprendiendo...</html>";
	private final String startStr   = "<html>Por favor seleccionar la posicion Inicial<br></html>";
	private final String travelStr  = "<html>Simulando<br></html>";
	private final String happyStr   = "<html>El Agente llego a la meta<br>:)</html>";
	private final String badStr		= "<html>NO SE PUEDE ALCANZAR LA META<br> :(</html>";
	private final String newStr		= "<html>Nueva simulacion<br></html>";
	private final String loadStr	= "<html>Nuevo mapa cargado<br></html>";

	// Estados de la simulacion
	private boolean initState;
	private boolean startState;
	private boolean travelState;
	private boolean happyEnd;
	private boolean badEnd;
	private boolean loadState;
	
        //Distancia recorrida para llegar a la meta
	private int travelDistance;
        
        int CantEx = 0;
        int CantBu = 0;
        int CantMa = 0;
        int CantNeu = 0;
        int CantPar = 0;

        public static double acumQ;
        
        
    public Interfaz() {
       
        initComponents();
       //fijando los valores para la simulacion
        fijarValores();
        acumQ = 0;   
        //UI Interfaz Grafica
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(Interfaz.class.getName()).log(Level.SEVERE, null, ex);
            }
       
       //Desactivo los botones que no deberia poder utilizar
       jComboBox2.setEnabled(false);
       jTextField1.setEnabled(false);
       jTextField2.setEnabled(false);
       jTextField3.setEnabled(false);
       jTextField4.setEnabled(false);
       jTextField5.setEnabled(false);
       jTextField6.setEnabled(false);
       jTextField7.setEnabled(false);
       jButton1.setEnabled(false);
       jButton5.setEnabled(false);
       jButton4.setEnabled(false);
       jButton6.setEnabled(false);
       jPanel1.setVisible(false);
       

       //inicializo las variables que me definiran el tamaño
       x_fila = 0; y_colu = 0; tamaño = 0; 

        //Fijo el tamaño de la ventana
       tamaño();
       
       //defino si los elementos se cargaran aleatoriamente o manualmente la primera vez
       eleSeteoElem();
       
       //fijando el tamaño de la grilla
       jPanel1.setLayout(new GridLayout(x_fila, y_colu));

        //Ubicar la ventana
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
	setLocation((dimension.width - getWidth())/2, 10);
        
                animCount = 0;		// Reset anim count
		travelDistance = 0; // Reset travel distance 
		
		// Inicializando Variables de la animacion
		initState	= true;	
		startState	= false;	
		travelState	= false;
		happyEnd	= false;
		badEnd  	= false;
		loadState	= false;

		// Datos necesarios para la parte grafica de la Matriz Q
		qwindow = new MatrizQVisual();
    	        qwindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		qwindow.initialize();
		qwindow.setVisible(false);

		startAnimation(); // Comenzar la animacion

		jTextArea1.append("TRABAJO PRACTICO INTEGRADOR DE IA\n");
                jTextArea1.append("Aprendizaje por Refuerzo\n");
		jTextArea1.append("Grupo N° 9\n");         
    }

    // Metodo para dibujar el Mapa
    private void dibujarMapa()
    {
        iconos = new Dibujo[tamaño];

        for(int i = 0; i < tamaño; i++){
                    iconos[i] = new Dibujo(i);	

                    jPanel1.add(iconos[i]);
                    iconos[i].setEnabled(false);
        }	
        dibujarIcono();    
    }
   
    //Metodo para Dibujar los iconos
    private void dibujarIcono(){
        for(int i = 0; i < tamaño; i++)
            iconos[i].dibujarIcono();
    }

    // Simulacion 
	private void simular()
	{	
               
		
		if(animCount % 100 == 0) 
		{			
			if(Dibujo.agenteListo() && !happyEnd && !badEnd)
			{
							
				
				startState = false; 
				travelState = true; 
				             
				jTextArea1.append("Pasos hasta la meta..... " + travelDistance + "\n");
				travelDistance++; 
				//moverse
                                utialize();
                                //actualizar iconos
                                dibujarIcono();
                                
				
				// Comprobar si es el estado final
				if(IS_GOAL())
				{
					
					applyHappyEnd(); 
					jTextArea1.append("Se a alcanzado la meta en " + travelDistance + " pasos.\n");
					travelDistance = 0;
					travelState = false; 
					happyEnd = true;	 
					
				}
				
				// Comprobar si no se alcanza el estado final
				else if(IS_DANGER())
				{
					
                                        applyBadEnd(); 
					jTextArea1.append("No puede se puede alcanzar la meta\n");
					travelDistance = 0;
					travelState = false; 
					badEnd = true;		
					
				}
				
				
				jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
			}
		}
		animCount++;
	}

	public void startAnimation()
	{
		if(animationTimer == null)
		{	
			animationTimer = new Timer(animationDelay, this);
			animationTimer.start();
		}
	}
		
	public void stopAnimation()
	{ 
		animationTimer.stop();
               
	}
        
	public void actionPerformed(ActionEvent arg0)
	{
		//correr simulacion
		simular();
	}

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox1 = new javax.swing.JComboBox();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jButton3 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Politica ");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Greedy", "Softmax", "Random" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jTextField1.setText("0.1");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("Aprender");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setText("Reseteo");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Simular");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("MatrizQ");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel3.setText("Datos Requeridos");

        jLabel7.setText("Gamma");

        jTextField2.setText("0.5");
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jLabel8.setText("Episodios");

        jTextField3.setText("5000");
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jLabel9.setText("Estado Final");

        jTextField4.setText("1000000");
        jTextField4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField4ActionPerformed(evt);
            }
        });

        jLabel10.setText("Excelente");

        jTextField5.setText("1000");
        jTextField5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField5ActionPerformed(evt);
            }
        });

        jLabel11.setText("Bueno");

        jTextField6.setText("800");
        jTextField6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField6ActionPerformed(evt);
            }
        });

        jLabel12.setText("Malo");

        jTextField7.setText("500");
        jTextField7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField7ActionPerformed(evt);
            }
        });

        jLabel4.setText("e");

        jLabel6.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel6.setText("DATOS PARA LA SIMULACION");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jLabel7)
                                            .addGap(22, 22, 22)
                                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(jLabel8))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel10)
                                        .addComponent(jLabel12))
                                    .addGap(10, 10, 10)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(jLabel9)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(10, 10, 10)
                                            .addComponent(jLabel11)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(38, 38, 38)
                                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(38, 38, 38)
                                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(jLabel3))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel6)
                .addGap(12, 12, 12)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGap(8, 8, 8)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel8)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel7)))
                .addGap(11, 11, 11)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel11)
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(11, 11, 11)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Tamaño ");

        jButton2.setText("Generar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel14.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel14.setText("GENERAR MAPA");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Excelente", "Bueno", "Malo", "Pared", "Final", "Neutro" }));
        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "6x6", "7x7", "8x8", "9x9", "10x10" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jRadioButton1.setText("Automatico");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("Manual");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Resetear");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Carga de datos");

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel13.setText("Tipo");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jRadioButton2)
                                    .addComponent(jRadioButton1))
                                .addGap(18, 18, 18)
                                .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(36, 36, 36)
                                .addComponent(jLabel13))))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(71, 71, 71)
                        .addComponent(jLabel14)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel14)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(jLabel13)))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jRadioButton2))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jComboBox3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setMaximumSize(new java.awt.Dimension(600, 600));
        jPanel1.setMinimumSize(new java.awt.Dimension(600, 600));
        jPanel1.setName(""); // NOI18N
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 516, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 161, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

       jComboBox2.setEnabled(true);
       jTextField1.setEnabled(true);
       jTextField2.setEnabled(true);
       jTextField3.setEnabled(true);
       
       jTextField5.setEnabled(true);
       jTextField6.setEnabled(true);
       jTextField7.setEnabled(true);
       jButton1.setEnabled(true);
       jButton5.setEnabled(true);
       jButton4.setEnabled(true);
       jButton6.setEnabled(true);
       jPanel1.setVisible(true);
        
       jButton2.setEnabled(false);
       jButton3.setEnabled(true);
       jComboBox1.setEnabled(false);
       jRadioButton1.setEnabled(false);
       jRadioButton2.setEnabled(false);
       
       jButton1.setEnabled(true);
       
       //fijar el tamaño de la ventana
       tamaño(); 
       
       //eleccion del metedo de ingreso de datos
       eleSeteoElem();
        
       //genero un nuevo mapa dependiendo si es manual o aleatorio
       mapa = new Mapa(cargaAleatManual);
         
      //dibujar el mapa
      dibujarMapa();
       
        jPanel1.setVisible(true);
        jPanel1.setLayout(new GridLayout(x_fila,y_colu));
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

       jComboBox2.setEnabled(false);
       jComboBox1.setEnabled(false);
       jComboBox3.setEnabled(false);
       jTextField1.setEnabled(false);
       jTextField2.setEnabled(false);
       jTextField3.setEnabled(false);
       jTextField4.setEnabled(false);
       jTextField5.setEnabled(false);
       jTextField6.setEnabled(false);
       jTextField7.setEnabled(false);
      
 
        
        jButton6.setEnabled(true);        
        initState = false;				
	jTextArea1.append("Espere un momento, se esta Aprendiendo\n"); 
        
       
	jPanel1.setEnabled(true);
	update(getGraphics());
	long startTime = System.currentTimeMillis();
        
         jTextArea1.append("Tamaño de la matriz "+ String.valueOf(x_fila -2)+ "x"+String.valueOf(y_colu-2)+"\n" );
        
        for(int i1 = 0; i1<tamaño;i1++){
            if(mapa.map[i1] == 'E') CantEx = CantEx +1; 
            else if (mapa.map[i1] == 'B') CantBu = CantBu +1;
            else if (mapa.map[i1] == 'M') CantMa = CantMa +1;
            else if (mapa.map[i1] == ' ') CantNeu = CantNeu +1;
            else if (mapa.map[i1] == '#') CantPar = CantPar +1;
            
        }
        
        jTextArea1.append("Cantidad de Excelentes "+String.valueOf(CantEx)+"\n");
        jTextArea1.append("Cantidad de Buenos "+ String.valueOf(CantBu)+"\n");
        jTextArea1.append("Cantidad de Malos "+ String.valueOf(CantMa)+"\n");
        jTextArea1.append("Cantidad de Neutros "+ String.valueOf(CantNeu)+"\n");
        jTextArea1.append("Cantidad de Paredes "+String.valueOf(CantPar-((x_fila*2)+((y_colu-2)*2)))+"\n");
        
        //Crear la matrizQ
        MatrizQ MQ = MatrizQ.getInstance();
        
        //Aprender
        MQ.qlearning();

        jTextArea1.append(badStr);
	jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
        
        
        
	//Hacer visible el mapa una vez que aprendio
        for(int i = 0; i < iconos.length; i++)
		iconos[i].setEnabled(true);
	
	startState = true; 
      
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
      
	animCount = 0;		
	travelDistance = 0;

	startState	= true;	
	travelState	= false;
	happyEnd	= false;
	badEnd  	= false;

        llegada	 = false;
        MatrizQ.llegada_vr = false;
	peligro	 = false;
        MatrizQ.peligro = false;
  
        Dibujo.resetearAgenteListo();

	jTextArea1.append("Reseteo.\n");
	jTextArea1.append("Puedes simular de nuevo.\n");
        
        for(int i= 0; i<tamaño;i++){
            if(mapa.map[i] == 'H') mapa.map[i] = 'G';
        }
	
	
	jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // Mostrar la matriz Q
	qwindow.setVisible(true);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed
        boolean estadofinalsimulacion = false;
      //Tipo de atributo a poner en el mapa
        switch (jComboBox3.getSelectedIndex()){
          case 0: estados = 0; break; //Excelente
          case 1: estados = 1; break; //Bueno
          case 2: estados = 2; break; //Malo
          case 3: estados = 3; break; //Pared
          
          //Primera prueba de estado final
          //case 4: //for (int i = 0; i<tamaño;i++){
//                   //    if(mapa.map[i] == MatrizQ.FINAL){
//                   //        estadofinalsimulacion = true;}
//                   //  } 
//                     if (estadofinalsimulacion){
//                       JOptionPane.showMessageDialog(this, "Ya existe un estado final", "ERROR", JOptionPane.ERROR_MESSAGE);
//                       
//                     }else{
//                         estados = 4;
//          
//                }break; //Final
          case 4: estados = 4; break; //Estado final
          case 5: estados = 5; break; //Neutro
      }
        
    
    }//GEN-LAST:event_jComboBox3ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        //Elegir la politica de accion para moverse
        switch (jComboBox2.getSelectedIndex()){
          case 0: jTextField1.setEnabled(true); jLabel4.setText("e");break;
          case 1: jTextField1.setEnabled(true); jLabel4.setText("t");break;
          case 2: jTextField1.setEnabled(false); jLabel4.setText(" ");break;
        }
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
            
        // Reset everything to let the user select a correct map
			initState	= false;
			startState	= false;	
			travelState	= false;
			happyEnd	= false;
			badEnd  	= false;
			loadState	= true; // User must load a new map

			Dibujo.agenteListo();

			jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength()); 
      
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
       
       
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField4ActionPerformed
        
    }//GEN-LAST:event_jTextField4ActionPerformed

    private void jTextField7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField7ActionPerformed
        
    }//GEN-LAST:event_jTextField7ActionPerformed

    private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField5ActionPerformed
       
    }//GEN-LAST:event_jTextField5ActionPerformed

    private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField6ActionPerformed
        
    }//GEN-LAST:event_jTextField6ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
    if (jRadioButton2.isSelected() == true){
            jRadioButton1.setSelected(false);
            jComboBox3.setEnabled(true);}
    }//GEN-LAST:event_jRadioButton2ActionPerformed
    
    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        if (jRadioButton1.isSelected() == true){
            jRadioButton2.setSelected(false);
            }
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       jPanel1.removeAll();
       jPanel1.setVisible(false);
       jButton2.setEnabled(true);
       jButton3.setEnabled(false);
       
       jComboBox1.setEnabled(true);
       jRadioButton1.setEnabled(true);
       jRadioButton2.setEnabled(true);
       jComboBox3.setEnabled(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    public static  int getEstado(){
        return estados;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {}
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
      
        //</editor-fold>

        /* Create and display the form */
       public static void mainGui() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
                new Interfaz().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    // End of variables declaration//GEN-END:variables

    public void tamaño(){
        switch (jComboBox1.getSelectedIndex()){
            
            case 0: x_fila = 8; y_colu = 8; tamaño = x_fila * y_colu;break;
                
            case 1: x_fila = 9; y_colu = 9; tamaño = x_fila * y_colu;break; 
                
            case 2: x_fila = 10; y_colu = 10; tamaño = x_fila * y_colu;break;
                
            case 3: x_fila = 11; y_colu = 11; tamaño = x_fila * y_colu;break;
                
            case 4: x_fila = 12; y_colu = 12; tamaño = x_fila * y_colu;break;
                
          
        }
        
    }
    
   public static Mapa getMapa(){
       return mapa;
   }
   
   public void eleSeteoElem(){
      
       if(jRadioButton1.isSelected())
           cargaAleatManual = "aleatorio";
       
       if(jRadioButton2.isSelected())    cargaAleatManual = "manual";
           
           }
   
   public void fijarValores(){
       gamma = Double.parseDouble(jTextField2.getText());
       episodios = Integer.parseInt(jTextField3.getText());
       estadofinal = Integer.parseInt(jTextField4.getText());
       excelente = Integer.parseInt(jTextField5.getText());
       buena = Integer.parseInt(jTextField6.getText());
       mala = Integer.parseInt(jTextField7.getText());
       
       eptau = Double.parseDouble(jTextField1.getText());
       
       
       if(jComboBox2.getSelectedIndex() == 0){
           politica = 1;
          
       }else{
           if(jComboBox2.getSelectedIndex() == 1){
           politica = 2;
       }else{
            if(jComboBox2.getSelectedIndex() == 2){
                politica = 3;
            }
           
}
           
   
       }}
   
          public void utialize()
	{
		double[] qArray = new double[8];
		
		//int Rindex = getRobotIndex();
                
		 int VRindex = getRobotIndex();
                int movimiento=-1;
                double maximaRecompensa=0;
                int destination = 0;
                for(int i = 0; i < 7; i++)
		{
                    

                    if(i == arriba)	    destination = VRindex - Interfaz.x_fila; 
                    else if(i == arribaizq)   destination = VRindex - Interfaz.x_fila -1;
                    else if(i == arribader)   destination = VRindex - Interfaz.x_fila +1;
                    else if(i == abajo)  destination = VRindex + Interfaz.x_fila;
                    else if(i == abajoizq) destination = VRindex + Interfaz.x_fila -1;
                    else if(i == abajoder) destination = VRindex + Interfaz.x_fila + 1;
                    else if(i == izquierda)  destination = VRindex - 1;
                    else if(i == derecha) destination = VRindex + 1;

                    if(MatrizQ.getInstance().getQ_matrix()[destination] != MatrizQ.PARED){
                        if(MatrizQ.getInstance().getQ_matrix()[destination]>maximaRecompensa){
                            maximaRecompensa= MatrizQ.getInstance().getQ_matrix()[destination];
                            movimiento= i;
                            
                        }
                    }
		}
		
			if(movimiento== arriba)
			{
				moveUp();
				return;
			}
                        
                        else if(movimiento== arribaizq)
			{
				moveUpLeft();
				return;
			}
                        
                        else if(movimiento == arribader)
			{
				moveUpRight();
				return;
			}
                        
			else if(movimiento== abajo)
			{
				moveDown();
				return;
			}
                        
                        else if(movimiento == abajoizq)
			{
				moveDownLeft();
				return;
			}
                        
                        else if(movimiento== abajoder)
			{
				moveDownRight();
				return;
			}
                        
			else if(movimiento == izquierda)
			{
				moveLeft();
				return;
			}
			else if(movimiento == derecha)
			{
				moveRight(); 
				return;
			}
                      
                        iconos[destination].dibujarIcono();
		
	}
        
      

	public void applyHappyEnd()
	{
		Interfaz.getMapa().map[getRobotIndex()] = MatrizQ.LLEGADAFINAL;
	}
	
	 
	public void applyBadEnd()
	{
		Interfaz.getMapa().map[getRobotIndex()] = MatrizQ.PARED;
	}
	
	public boolean IS_DANGER()
	{
		return peligro;
	}

	public boolean IS_GOAL()
	{
		return llegada;
	}

   
   
  public int getRobotIndex()
	{
		for(int i = 0; i < Interfaz.tamaño; i++)
			if(Interfaz.getMapa().map[i] == AGENTE)
				return i;
		
		return -1; // Robot not found!!
	}
  
  // MOVE UP
	public boolean moveUp()
	{
		int index = getRobotIndex();
		
		if(index == -1) return false;
		
		int destination = index - Interfaz.x_fila;
		
		if(Interfaz.getMapa().map[destination] != PARED)
		{
			// Check GOAL and DANGER states
			if(Interfaz.getMapa().map[destination] == PARED) peligro = true;
			else if(Interfaz.getMapa().map[destination] == FINAL) llegada = true;
				
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.EXCELENTE) {
                            Interfaz.getMapa().map[index] = MatrizQ.EXCELENTE;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.BUENA){
                            Interfaz.getMapa().map[index] = MatrizQ.BUENA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.MALA){
                            Interfaz.getMapa().map[index] = MatrizQ.MALA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        Interfaz.getMapa().map[index] = NEUTRO; // Clear previous coordinate
			Interfaz.getMapa().map[destination] = AGENTE; // Update new coordinate
                        dibujarIcono();
			return true;
		}
		
		return false;
	}
        
         // MOVE UP/LEFT
	public boolean moveUpLeft()
	{
		int index = getRobotIndex();
		
		if(index == -1) return false;
		
		int destination = index - Interfaz.x_fila -1;
		
		if(Interfaz.getMapa().map[destination] != PARED)
		{
			// Check GOAL and DANGER states
			if(Interfaz.getMapa().map[destination] == PARED) peligro = true;
			else if(Interfaz.getMapa().map[destination] == FINAL) llegada = true;

                        else if(Interfaz.getMapa().map[destination]==MatrizQ.EXCELENTE) {
                            Interfaz.getMapa().map[index] = MatrizQ.EXCELENTE;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.BUENA){
                            Interfaz.getMapa().map[index] = MatrizQ.BUENA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.MALA){
                            Interfaz.getMapa().map[index] = MatrizQ.MALA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        Interfaz.getMapa().map[index] = NEUTRO; // Clear previous coordinate
			Interfaz.getMapa().map[destination] = AGENTE; // Update new coordinate
			dibujarIcono();
			return true;
		}
		
		return false;
	}
	
        // MOVE UP/RIGHT
	public boolean moveUpRight()
	{
		int index = getRobotIndex();
		
		if(index == -1) return false;
		
		int destination = index - Interfaz.x_fila +1;
		
		if(Interfaz.getMapa().map[destination] != PARED)
		{
			// Check GOAL and DANGER states
			if(Interfaz.getMapa().map[destination] == PARED) peligro = true;
			else if(Interfaz.getMapa().map[destination] == FINAL) llegada = true;
				
			else if(Interfaz.getMapa().map[destination]==MatrizQ.EXCELENTE) {
                            Interfaz.getMapa().map[index] = MatrizQ.EXCELENTE;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.BUENA){
                            Interfaz.getMapa().map[index] = MatrizQ.BUENA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.MALA){
                            Interfaz.getMapa().map[index] = MatrizQ.MALA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        Interfaz.getMapa().map[index] = NEUTRO; // Clear previous coordinate
			Interfaz.getMapa().map[destination] = AGENTE; // Update new coordinate
                        dibujarIcono();
			return true;
		}
		
		return false;
	}
        
        // MOVE DOWN
	public boolean moveDown()
	{
		int index = getRobotIndex();
		
		if(index == -1) return false;
		
		int destination = index + Interfaz.x_fila;
		
		if(Interfaz.getMapa().map[destination] != PARED)
		{
			// Check GOAL and DANGER states
			if(Interfaz.getMapa().map[destination] == PARED) peligro = true;
			else if(Interfaz.getMapa().map[destination] == FINAL) llegada = true;
			
			else if(Interfaz.getMapa().map[destination]==MatrizQ.EXCELENTE) {
                            Interfaz.getMapa().map[index] = MatrizQ.EXCELENTE;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.BUENA){
                            Interfaz.getMapa().map[index] = MatrizQ.BUENA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.MALA){
                            Interfaz.getMapa().map[index] = MatrizQ.MALA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        Interfaz.getMapa().map[index] = NEUTRO; // Clear previous coordinate
			Interfaz.getMapa().map[destination] = AGENTE; // Update new coordinate
                        dibujarIcono();
			return true;
		}
		
		return false;
	}
        
           // MOVE DOWN/LEFT
	public boolean moveDownLeft()
	{
		int index = getRobotIndex();
		
		if(index == -1) return false;
		
		int destination = index + Interfaz.x_fila -1;
		
		if(Interfaz.getMapa().map[destination] != PARED)
		{
			// Check GOAL and DANGER states
			if(Interfaz.getMapa().map[destination] == PARED) peligro = true;
			else if(Interfaz.getMapa().map[destination] == FINAL) llegada = true;
			
			else if(Interfaz.getMapa().map[destination]==MatrizQ.EXCELENTE) {
                            Interfaz.getMapa().map[index] = MatrizQ.EXCELENTE;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.BUENA){
                            Interfaz.getMapa().map[index] = MatrizQ.BUENA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.MALA){
                            Interfaz.getMapa().map[index] = MatrizQ.MALA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        Interfaz.getMapa().map[index] = NEUTRO; // Clear previous coordinate
			Interfaz.getMapa().map[destination] = AGENTE; // Update new coordinate
                        dibujarIcono();
			return true;
		}
		
		return false;
	}
        
        // MOVE DOWN/RIGHT
	public boolean moveDownRight()
	{
		int index = getRobotIndex();
		
		if(index == -1) return false;
		
		int destination = index + Interfaz.x_fila +1;
		
		if(Interfaz.getMapa().map[destination] != PARED)
		{
			// Check GOAL and DANGER states
			if(Interfaz.getMapa().map[destination] == PARED) peligro = true;
			else if(Interfaz.getMapa().map[destination] == FINAL) llegada = true;
			
			else if(Interfaz.getMapa().map[destination]==MatrizQ.EXCELENTE) {
                            Interfaz.getMapa().map[index] = MatrizQ.EXCELENTE;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.BUENA){
                            Interfaz.getMapa().map[index] = MatrizQ.BUENA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.MALA){
                            Interfaz.getMapa().map[index] = MatrizQ.MALA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        Interfaz.getMapa().map[index] = NEUTRO; // Clear previous coordinate
			Interfaz.getMapa().map[destination] = AGENTE; // Update new coordinate
                        dibujarIcono();
			return true;
		}
		
		return false;
	}
        
       
        
        
	// MOVE LEFT
	public boolean moveLeft()
	{
		int index = getRobotIndex();
		
		if(index == -1) return false;
		
		int destination = index - 1;
		
		if(Interfaz.getMapa().map[destination] != PARED)
		{
			// Check GOAL and DANGER states
			if(Interfaz.getMapa().map[destination] == PARED) peligro = true;
			else if(Interfaz.getMapa().map[destination] == FINAL) llegada = true;
			
			else if(Interfaz.getMapa().map[destination]==MatrizQ.EXCELENTE) {
                            Interfaz.getMapa().map[index] = MatrizQ.EXCELENTE;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.BUENA){
                            Interfaz.getMapa().map[index] = MatrizQ.BUENA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.MALA){
                            Interfaz.getMapa().map[index] = MatrizQ.MALA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        Interfaz.getMapa().map[index] = NEUTRO; // Clear previous coordinate
			Interfaz.getMapa().map[destination] = AGENTE; // Update new coordinate
                        dibujarIcono();
			return true;
		}
		
		return false;
	}
        
        // MOVE RIGHT
	public boolean moveRight()
	{
            
		int index = getRobotIndex();
		
		if(index == -1) return false;
		
		int destination = index + 1;
		
		if(Interfaz.getMapa().map[destination] != PARED )
		{
			// Check GOAL and DANGER states
			if(Interfaz.getMapa().map[destination] == PARED) peligro = true;
			else if(Interfaz.getMapa().map[destination] == FINAL) llegada = true;
			
			else if(Interfaz.getMapa().map[destination]==MatrizQ.EXCELENTE) {
                            Interfaz.getMapa().map[index] = MatrizQ.EXCELENTE;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.BUENA){
                            Interfaz.getMapa().map[index] = MatrizQ.BUENA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        else if(Interfaz.getMapa().map[destination]==MatrizQ.MALA){
                            Interfaz.getMapa().map[index] = MatrizQ.MALA;
                            Interfaz.getMapa().map[destination] = AGENTE;
                        }
                        Interfaz.getMapa().map[index] = NEUTRO; // Clear previous coordinate
			Interfaz.getMapa().map[destination] = AGENTE; // Update new coordinate
                        dibujarIcono();
			return true;
		}
		
		return false;
	}
	
        public static void acumularQ(double x){
            
            acumQ = acumQ + x;
           
            
        }
	
        
        
}
